package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.QuestionnaireAdapter;
import cn.otrue.patienthealthmanager.bean.QuestionnaireListModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 问卷回答发送情况列表
 * 
 * @author BGX
 * 
 */
public class QuestionnaireList extends BaseActivity implements
		IXListViewListener {
	private XListView xlv_question_list;
	private int currentPage;
	private List<QuestionnaireListModel> questionnaireList;
	private QuestionnaireAdapter adapter;
	private boolean isData;// 判断是否有数据
	public  static QuestionnaireList list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_questionnaire_list);
		initview();
		list=this;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();
			break;
		}
	}

	public void initview() {
		initTitleBar();
		questionnaireList = new ArrayList<QuestionnaireListModel>();
		xlv_question_list = (XListView) findViewById(R.id.xlv_question_list);
		currentPage = 1;
		xlv_question_list.setXListViewListener(this);
		xlv_question_list.setPullLoadEnable(false);
		getData();
	}

	private void initTitleBar() {
		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		// TextView tv_title_other = (TextView)
		// findViewById(R.id.tv_title_other);
		tv_title_title.setText(getString(R.string.question_query));
		// tv_title_other.setVisibility(View.VISIBLE);
		// tv_title_other.setText(getString(R.string.complete));
		// tv_title_other.setOnClickListener(this);
	}

	public void getData() {
		HashMap<String, Object> hash = new HashMap<String, Object>();
		hash.put("p_account", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setCurrentPage("" + currentPage);
		parameter.setPagesize("20");
		parameter.setData(hash);
		new HttpServer().post(HttpCons.QueryTheQuestionnaireList, parameter,
				handler);
	}

	private Handler handler = new ServerHandler() {
		public void onSuccess(Bundle data, String state, String msg) {
			String getdata = data.getString(HttpCons.DATA);
			if ("001".equals(state)) {
				List<QuestionnaireListModel> questionModel = new ArrayList<QuestionnaireListModel>();
				questionModel = HttpServer.gson.fromJson(getdata,
						new TypeToken<List<QuestionnaireListModel>>() {
						}.getType());
				questionnaireList.addAll(questionModel);
				if ((questionnaireList.size() == 0) && (isData == false)) {
					Toast.makeText(QuestionnaireList.this,
							getString(R.string.no_questionnaire),
							Toast.LENGTH_SHORT).show();
				} else {
					if (questionnaireList.size() >= 5) {
						xlv_question_list.setPullLoadEnable(true);
					}
					if (adapter == null) {
						adapter = new QuestionnaireAdapter(
								QuestionnaireList.this, questionnaireList);
						xlv_question_list.setAdapter(adapter);
					} else {
						adapter.notifyDataSetChanged();
					}
					isData = true;
				}
			} else {
				Toast.makeText(QuestionnaireList.this, msg, Toast.LENGTH_SHORT)
						.show();
			}
			onLoad();
			xlv_question_list
					.setOnItemClickListener(new AdapterView.OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							Intent detail = new Intent(QuestionnaireList.this,
									QuestionnaireDetailActivity.class);
							detail.putExtra("researchId",
									questionnaireList.get(arg2 - 1).researchId);
							startActivity(detail);
						}
					});

		};

		public void onFail(Bundle data, String state) {
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(QuestionnaireList.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)
			}
			onLoad();
		};
	};

	// 关闭刷新
	private void onLoad() {
		xlv_question_list.stopRefresh();
		xlv_question_list.stopLoadMore();
		xlv_question_list.setRefreshTime("刚刚");
	}

	@Override
	public void onRefresh() {
		currentPage = 1;
		questionnaireList.clear();
		getData();
	}

	@Override
	public void onLoadMore() {
		currentPage++;
		getData();
	}
}
